package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    GoodsService goodsService;

    @SaAdminCheckPermission("admin:goods:add")
    @PostMapping("")
    public Result addGoods(@RequestBody GoodsCommand goodsCommand) {
        goodsService.addGoods(goodsCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:goods:update")
    @PutMapping("")
    public Result updateGoods(@RequestBody GoodsCommand goodsCommand) {
        goodsService.updateGoods(goodsCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:goods:delete")
    @DeleteMapping("/{goodsId}")
    public Result deleteGoods(@PathVariable("goodsId") String goodId) {
        goodsService.deleteGoods(Long.valueOf(goodId));
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:goods:batchdelete")
    @DeleteMapping("/selected")
    public Result deleteGoodsSelected(@RequestBody BatchDeleteCommand batchDeleteCommand) {
        goodsService.deleteGoodsSelected(batchDeleteCommand);
        return Result.ok();
    }

    @PutMapping("/status/{goodsId}")
    public Result updateGoodsStatus(@PathVariable("goodsId") String goodId,
                                    @RequestParam("status") Integer status) {
        goodsService.updateGoodsStatus(Long.valueOf(goodId), status);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:goods:search")
    @GetMapping("/list")
    public Result<PageDTO<GoodsDTO>> getGoodsPage(GoodsPageRequest request) {
        PageDTO<GoodsDTO> goodsPage = goodsService.getGoodsPage(request);
        return Result.ok(goodsPage);
    }

}
